﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel;

namespace Twinkle.Data
{
    public class Storage
    {
        Dictionary<string, Tweak.TUser> userStorage = new Dictionary<string, Twinkle.Tweak.TUser>();

        public Tweak.TUser LookupUser(string id)
        {
            if (userStorage.ContainsKey(id))
                return userStorage[id];
            else
                return null;
        }

        public void RegistUser(Tweak.TUser user)
        {
            if (user == null || user.Id == null)
                return;
            try
            {
                if (userStorage.ContainsKey(user.Id))
                    userStorage[user.Id] = user;
                else
                    userStorage.Add(user.Id, user);
            }
            catch (NullReferenceException) { }
        }

        public IEnumerable<string> GetUsers()
        {
            foreach(var k in userStorage.Keys)
            {
                yield return k;
            }
        }

        List<string> hashStorage = new List<string>();
        public bool AddHashtag(string hash)
        {
            if (String.IsNullOrEmpty(hash))
                return false;
            hash = hash.TrimStart('#');
            if (String.IsNullOrEmpty(hash))
                return false;
            if (hashStorage.Contains(hash))
                return false;

            hashStorage.Add(hash);
            return true;
        }

        public IEnumerable<string> GetHashtags()
        {
            return hashStorage;
        }

        private object uidResolverLocker = new object();
        List<KeyValuePair<string, ulong>> uidResolverStorage = new List<KeyValuePair<string, ulong>>();

        public bool AddSidUid(string sid, ulong uid)
        {
            lock (uidResolverLocker)
            {
                foreach (var u in uidResolverStorage)
                {
                    if (u.Key == sid)
                    {
                        return false;
                    }
                }
                uidResolverStorage.Add(new KeyValuePair<string, ulong>(sid, uid));
                return true;
            }
        }

        public IEnumerable<ulong> GetRegisteredUids()
        {
            return from i in uidResolverStorage
                   select i.Value;
        }

        public ulong GetUid(string sid)
        {
            lock (uidResolverLocker)
            {
                foreach (var u in uidResolverStorage)
                {
                    if (u.Key == sid)
                    {
                        return u.Value;
                    }
                }
                return 0;
            }
        }

        public string GetSid(ulong uid)
        {
            lock (uidResolverLocker)
            {
                foreach (var u in uidResolverStorage)
                {
                    if (u.Value == uid)
                    {
                        return u.Key;
                    }
                }
                return null;
            }
        }
    }
}
